package com.neusoft.elmboot.controller;

import java.util.List;

import com.neusoft.elmboot.dto.FoodDTO;
import com.neusoft.elmboot.service.ex.DataNotFoundException;
import com.neusoft.elmboot.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.neusoft.elmboot.po.Food;
import com.neusoft.elmboot.service.FoodService;

@RestController
@RequestMapping("/foods")
public class FoodController {
	public static final int OK = 200;
	public static final int DATA_NOT_FOUND = 204;

	@Autowired
	private FoodService foodService;

	@GetMapping("/{businessId}")
	public JsonResult<List<Food>> listFoodByBusinessId(@PathVariable Integer businessId) throws Exception{
		JsonResult<List<Food>> result = new JsonResult<List<Food>>();
		try{
			List<Food> data = foodService.listFoodByBusinessId(businessId);
			result = new JsonResult<List<Food>>(OK, data);
			result.setMessage("获取商家食物列表成功");
			return result;
		}catch(DataNotFoundException e){
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		}
	}

	@GetMapping("/search")
	public JsonResult<List<Food>> searchFood(@RequestBody FoodDTO foodDTO) throws Exception{
		JsonResult<List<Food>> result = new JsonResult<List<Food>>();
		try{
			List<Food> data = foodService.searchFood(foodDTO);
			result = new JsonResult<List<Food>>(OK, data);
			result.setMessage("搜索食物成功!");
			return result;
		}catch(DataNotFoundException e){
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		}
	}



}
